﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcVehicleInsurance.Models;

namespace MvcVehicleInsurance.Areas.Admin.Controllers
{
    public class CusController : Controller
    {

        private DataClassesVehicleDataContext data = new DataClassesVehicleDataContext();
        //
        // GET: /Admin/Cus/

        public ActionResult Index()
        {
            List<CustomerRecord> ctm = (from ct in data.CustomerRecords select ct).ToList();
            if(ModelState.IsValid)
            {
                return View(ctm);
            }
            return View();
        }

        //
        // GET: /Admin/Cus/Details/5

        public ActionResult Details(int id)
        {
            CustomerRecord ctd = (from ctt in data.CustomerRecords.Where(ct => ct.customerID == id) select ctt).First();
            if(ModelState.IsValid)
            {
                return View(ctd);
            }
            return View();
        }

        //
        // GET: /Admin/Cus/Create

        public ActionResult Create()
        {
            CustomerRecord ctc = new CustomerRecord();
            return View();
        } 

        //
        // POST: /Admin/Cus/Create

        [HttpPost]
        public ActionResult Create(CustomerRecord ctc)
        {
           if(ModelState.IsValid)
           {
               CustomerRecord ge = new CustomerRecord();
               ge.customerUsername = ctc.customerUsername;
               ge.customerPassword = ctc.customerPassword;
               ge.customerFullName = ctc.customerFullName;
               ge.customerAddress = ctc.customerAddress;
               ge.customerEmail = ctc.customerEmail;
               ge.customerPhoneNumber = ctc.customerPhoneNumber;
               ge.customerAddProve = ctc.customerAddProve;

               data.CustomerRecords.InsertOnSubmit(ctc);
               data.SubmitChanges();
               RedirectToAction("Index");
           }
           return View();

        }
        
        //
        // GET: /Admin/Cus/Edit/5
 
        public ActionResult Edit(int id)
        {
            CustomerRecord cte = new CustomerRecord();
            return View();
        }

        //
        // POST: /Admin/Cus/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, CustomerRecord cte)
        {
            try
            {
                CustomerRecord edit = data.CustomerRecords.Where(ct => ct.customerID == id).First();

                edit.customerUsername = cte.customerUsername;
                edit.customerPassword = cte.customerPassword;
                edit.customerFullName = cte.customerFullName;
                edit.customerAddress = cte.customerAddress;
                edit.customerEmail = cte.customerEmail;
                edit.customerPhoneNumber = cte.customerPhoneNumber;
                edit.customerAddProve = cte.customerAddProve;

                data.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Cus/Search/5

        public ActionResult Search()
        {
            List<CustomerRecord> cts = new List<CustomerRecord>();
            return View(cts);
        }


        [HttpPost]
        public ActionResult Search(string Name)
        {                                                   
            List<CustomerRecord> cts = (from ct in data.CustomerRecords.Where(ctt =>        ctt.customerFullName.Contains(Name)) select ct).ToList();
            return View(cts);
        }
 
      
    }
}
